/*
 * 诚信查询
 * @author EX-XIEYUWEN001
 * Filename:    IntegrityMedicalServiceImpl.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014年10月30日
 */
package com.paic.mhis.hcpms.trust.biz.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.common.util.PageUtil;
import com.paic.mhis.hcpms.audit.constants.AuditConstants;
import com.paic.mhis.hcpms.common.storage.biz.service.HcpmsStorageService;
import com.paic.mhis.hcpms.common.storage.dto.HcpmsStorageDTO;
import com.paic.mhis.hcpms.trust.biz.service.InsuredMedicalDetailService;
import com.paic.mhis.hcpms.trust.biz.service.IntegrityMedicalService;
import com.paic.mhis.hcpms.trust.dto.IntegrityMedicalDTO;
import com.paic.mhis.hcpms.trust.dto.IntegrityMedicalDoctorDTO;
import com.paic.mhis.hcpms.trust.dto.IntegrityMedicalOrgDTO;
import com.paic.mhis.hcpms.trust.dto.PointCutDTO;
import com.paic.mhis.hcpms.trust.intergration.dao.IntegrityMedicalDAO;
import com.paic.mhis.hcpms.trust.util.TrustConstants;
import com.paic.mhis.hcpms.warning.dto.HcpmsWarnsDTO;
import com.paic.mhis.hcpms.warning.dto.WarningDeductionDTO;
import com.paic.mhis.hcpms.warning.util.WarnConstants;
import com.paic.pafa.exceptions.BusinessException;

@Component("integrityMedicalService")
public class IntegrityMedicalServiceImpl implements IntegrityMedicalService {
	@Autowired
	private IntegrityMedicalDAO integrityMedicalDao;

	@Autowired
	private HcpmsStorageService hcpmsStorageService;

	@Autowired
	private InsuredMedicalDetailService insuredMedicalDetailService;

	@Override
	public List<IntegrityMedicalDTO> findIntegrityMedical(int pageSize, int currentPage, Map<String, Object> paramMap)
			throws BusinessException {
		// 如果没有分页值则初始化值
		PageUtil.assemblePageParam(pageSize, currentPage, paramMap);
		return integrityMedicalDao.findIntegrityMedical(paramMap);
	}

	@Override
	public int countIntegrityMedical(Map<String, Object> paramMap) throws BusinessException {
		return integrityMedicalDao.countIntegrityMedical(paramMap);
	}

	@Override
	public List<IntegrityMedicalDTO> findIntegrityMedicalDesc(Map<String, Object> paramMap) throws BusinessException {
		return integrityMedicalDao.findIntegrityMedicalDesc(paramMap);
	}

	@Override
	public List<IntegrityMedicalDTO> findIntegrityAuditAndApproveMedicalDesc(Map<String, Object> paramMap)
			throws BusinessException {
		return integrityMedicalDao.findIntegrityAuditAndApproveMedicalDesc(paramMap);
	}

	@Override
	public List<IntegrityMedicalDTO> findIntegrityMedicalMark(Map<String, Object> paramMap) throws BusinessException {
		return integrityMedicalDao.findIntegrityMedicalMark(paramMap);
	}

	@Override
	public List<IntegrityMedicalDoctorDTO> findIntegrityDoctor(Map<String, Object> paramMap) throws BusinessException {
		return integrityMedicalDao.findIntegrityDoctor(paramMap);
	}

	@Override
	public List<IntegrityMedicalOrgDTO> findIntegrityOrg(Map<String, Object> paramMap) throws BusinessException {
		return integrityMedicalDao.findIntegrityOrg(paramMap);
	}

	@Override
	public boolean addIntegrityPoint(PointCutDTO pointCut) {
		try {
			integrityMedicalDao.addIntegrityPoint(pointCut);
		} catch (BusinessException e) {
			return false;
		}
		return true;
	}

	@Override
	public boolean addIntegrityPoints(JSONObject transferJson, String userName, String caseNo, String caseType)
			throws BusinessException {
		if (transferJson == null) {
			return false;
		}
		PointCutDTO pointCut = new PointCutDTO();
		pointCut = this.getPointCutDTOTemp(transferJson, pointCut, caseNo, caseType);
		JSONArray jsonArr = (JSONArray) transferJson.remove("warnMarkTempDetails");
		boolean isTrue = (jsonArr == null || (StringUtils.isEmpty(pointCut.getHappenDate())));
		if (isTrue) {
			return false;
		}
		List<PointCutDTO> pointCuts = new ArrayList<PointCutDTO>();
		if (jsonArr.size() > 0) {
			pointCuts = this.getPointCuts(pointCuts, jsonArr, userName, pointCut);
			if (pointCuts.size() > 0) {
				integrityMedicalDao.addIntegrityPointTemps(pointCuts);
			}
			return true;
		}
		return false;
	}

	@Override
	public List<IntegrityMedicalDTO> findIntegrityMedicalTotal(int pageSize, int currentPage,
			Map<String, Object> paramMap) throws BusinessException {
		// 如果没有分页值则初始化值
		PageUtil.assemblePageParam(pageSize, currentPage, paramMap);
		return integrityMedicalDao.findIntegrityMedicalTotal(paramMap);
	}

	@Override
	public int countIntegrityMedicalTotal(Map<String, Object> paramMap) throws BusinessException {
		return integrityMedicalDao.countIntegrityMedicalTotal(paramMap);
	}

	@Override
	public List<IntegrityMedicalDTO> findIntegrityMedicalReport(int pageSize, int currentPage,
			Map<String, Object> paramMap) throws BusinessException {
		// 如果没有分页值则初始化值
		PageUtil.assemblePageParam(pageSize, currentPage, paramMap);
		return integrityMedicalDao.findIntegrityMedicalReport(paramMap);
	}

	@Override
	public int countIntegrityMedicalReport(Map<String, Object> paramMap) throws BusinessException {
		return integrityMedicalDao.countIntegrityMedicalReport(paramMap);
	}

	@Override
	public boolean addIntegrityPointTemps(JSONObject transferJson, String userName) throws BusinessException {
		if (transferJson == null) {
			return false;
		}
		JSONArray jsonArr = (JSONArray) transferJson.remove("warnMarkTempDetails");
		if (jsonArr == null) {
			return false;
		}
		List<PointCutDTO> pointCuts = new ArrayList<PointCutDTO>();
		if (jsonArr.size() > 0) {
			pointCuts = this.getPointCuts(pointCuts, jsonArr, userName, null);
			if (pointCuts.size() > 0) {
				integrityMedicalDao.addIntegrityPointTemps(pointCuts);
			}
			return true;
		}
		return false;
	}

	/**
	 * 组织记分信息
	 * 
	 * @param jsonArr
	 * @param pointCuts
	 * @param userName
	 * @param pointCut
	 * @return
	 */
	private List<PointCutDTO> addPointCutList(JSONArray jsonArr, List<PointCutDTO> pointCuts, String userName,
			PointCutDTO pointCut, boolean storageIsTrue, boolean delIsTrue) {
		if (jsonArr == null) {
			return pointCuts;
		}
		if (jsonArr.size() > 0) {
			for (int j = 0; j < jsonArr.size(); j++) {
				PointCutDTO dto = new PointCutDTO();
				JSONObject tempJson = jsonArr.getJSONObject(j);
				dto = JSONObject.toJavaObject(tempJson, PointCutDTO.class);
				if (dto != null) {
					if (pointCut != null) {
						dto.setCaseNo(pointCut.getCaseNo());
						dto.setHappenDate(pointCut.getHappenDate());
						dto.setPunishDateStart(pointCut.getPunishDateStart());
						dto.setPunishDateEnd(pointCut.getPunishDateEnd());
					}
					dto.setCreatedBy(userName);
					dto.setUpdatedBy(userName);
					dto.setDelIsTrue(delIsTrue);
					dto.setStorageIsTrue(storageIsTrue);// 是否暂存记分
														// 数据来源true:暂存记分，false:非暂存记分（查询使用）
					if (delIsTrue && storageIsTrue && StringUtils.isEmpty(dto.getIdPointCut())) {// 暂存删除的记分不反回前端
						continue;
					} else {
						pointCuts.add(dto);
					}
				}
			}
		}
		return pointCuts;
	}

	@Override
	public List<PointCutDTO> findPointCuts(Map<String, Object> paramMap) throws BusinessException {
		return integrityMedicalDao.findPointCuts(paramMap);
	}

	@Override
	public List<PointCutDTO> findPointCutTemps(Map<String, Object> paramMap) throws BusinessException {
		return integrityMedicalDao.findPointCutTemps(paramMap);
	}

	@Override
	public boolean addIntegrityPointTemps(Map<String, Object> paramMap) throws BusinessException {
		String warnNo = (String) paramMap.get("warnNo");
		String caseType = (String) paramMap.get("caseType");
		if (!"02".equals(caseType)) {
			paramMap.put("happenDate", null);
		}
		if (StringUtils.isEmpty(warnNo)) {
			return false;
		}
		integrityMedicalDao.addPointCut(paramMap);
		return true;
	}

	@Override
	public boolean deletePointCut(JSONArray jsonArr) throws BusinessException {
		List<PointCutDTO> ids = new ArrayList<PointCutDTO>();
		if (jsonArr == null) {
			return false;
		}
		if (jsonArr.size() > 0) {
			for (int j = 0; j < jsonArr.size(); j++) {
				JSONObject tempJson = jsonArr.getJSONObject(j);
				PointCutDTO dto = new PointCutDTO();
				dto = JSONObject.toJavaObject(tempJson, PointCutDTO.class);
				if (dto != null) {
					ids.add(dto);
				}
			}
		}
		if (ids.size() > 0) {
			integrityMedicalDao.deletePointCut(ids);
		}
		return true;
	}

	@Override
	public boolean saveIntegrityPoints(JSONObject transferJson, String userName) throws BusinessException {
		if (transferJson == null) {
			return false;
		}
		JSONArray jsonArr = (JSONArray) transferJson.remove("warningMarks");
		if (jsonArr == null) {
			return false;
		}
		List<PointCutDTO> pointCuts = new ArrayList<PointCutDTO>();
		if (jsonArr.size() > 0) {
			pointCuts = this.getPointCuts(pointCuts, jsonArr, userName, null);
			if (pointCuts.size() > 0) {
				integrityMedicalDao.addIntegrityPoints(pointCuts);
			}
			return true;
		}
		return false;
	}

	/**
	 * 操作记分
	 * 
	 * @param pointCuts
	 * @param jsonArr
	 * @param userName
	 * @param pointCut
	 * @return
	 * @throws BusinessException
	 */
	private List<PointCutDTO> getPointCuts(List<PointCutDTO> pointCuts, JSONArray jsonArr, String userName,
			PointCutDTO pointCut) throws BusinessException {
		JSONObject tempJson = jsonArr.getJSONObject(0);
		// 删除
		JSONArray deductionDeletes = (JSONArray) tempJson.remove("deductionDeletes");
		this.deletePointCut(deductionDeletes);
		pointCuts = this.getPointCutParams(pointCuts, jsonArr, userName, pointCut, tempJson, false);
		return pointCuts;
	}

	/**
	 * 解析记分信息
	 * 
	 * @param pointCuts
	 * @param jsonArr
	 * @param userName
	 * @param pointCut
	 * @param tempJson
	 * @param
	 * @return
	 */
	private List<PointCutDTO> getPointCutParams(List<PointCutDTO> pointCuts, JSONArray jsonArr, String userName,
			PointCutDTO pointCut, JSONObject tempJson, boolean storageIsTrue) {
		if (storageIsTrue) {
			// 删除
			JSONArray deductionDeletes = (JSONArray) tempJson.remove("deductionDeletes");
			pointCuts = this.addPointCutList(deductionDeletes, pointCuts, userName, pointCut, storageIsTrue, true);// 清理暂存删除时遗留数据
		}
		// 机构
		JSONArray deducitonMedicals = (JSONArray) tempJson.remove("deducitonMedicals");
		pointCuts = this.addPointCutList(deducitonMedicals, pointCuts, userName, pointCut, storageIsTrue, false);
		// 医务人员
		JSONArray deducitonStaffs = (JSONArray) tempJson.remove("deducitonStaffs");
		pointCuts = this.addPointCutList(deducitonStaffs, pointCuts, userName, pointCut, storageIsTrue, false);
		// 参保人员
		JSONArray deducitonInsurs = (JSONArray) tempJson.remove("deducitonInsurs");
		pointCuts = this.addPointCutList(deducitonInsurs, pointCuts, userName, pointCut, storageIsTrue, false);
		return pointCuts;
	}

	@Override
	public String mediCalContentPoint(Map<String, Object> paramMap) throws BusinessException {
		return integrityMedicalDao.mediCalContentPoint(paramMap);
	}

	@Override
	public List<WarningDeductionDTO> findIdStandardScores(Map<String, Object> paramMap) throws BusinessException {
		return integrityMedicalDao.findIdStandardScores(paramMap);
	}

	/**
	 * 组织暂存临时记分数据
	 * 
	 * @param paramMap
	 * @return
	 */
	private List<PointCutDTO> getPointCutTempStorages(Map<String, Object> paramMap, List<PointCutDTO> pointCutTemps) {
		if (null != pointCutTemps) {
			if (pointCutTemps.size() > 0) {
				pointCutTemps = this.getPointCutTemps(paramMap, pointCutTemps);
			} else {
				pointCutTemps = this.getPointCutTemps(paramMap, pointCutTemps);
			}
		}
		return pointCutTemps;
	}

	private List<PointCutDTO> getPointCutTemps(Map<String, Object> paramMap, List<PointCutDTO> pointCutTemps) {
		JSONObject transferJson = this.getPointCutTempJson(paramMap);
		if (transferJson != null) {
			JSONArray jsonArr = (JSONArray) transferJson.remove("warnMarkTempDetails");
			if (jsonArr != null) {
				if (jsonArr.size() > 0) {
					JSONObject tempJson = jsonArr.getJSONObject(0);
					pointCutTemps = this.getPointCutParams(pointCutTemps, jsonArr, null, null, tempJson, true);
				}
			}
		}
		return pointCutTemps;
	}

	/**
	 * 获取暂存临时记分数据
	 * 
	 * @param paramMap
	 * @return
	 */
	private JSONObject getPointCutTempJson(Map<String, Object> paramMap) {
		JSONObject transferJson = null;
		String caseType = (String) paramMap.get("caseType");
		String caseNo = (String) paramMap.get("caseNo");
		String userName = (String) paramMap.get("userName");
		if (StringUtils.isNotEmpty(caseType) && StringUtils.isNotEmpty(caseNo)) {
			HcpmsStorageDTO dto = new HcpmsStorageDTO();
			dto.setUserId(userName);// userName
			dto.setModuleType(caseType);
			dto.setBusinessId(caseNo);
			dto.setBusinessType(WarnConstants.WARN_BUSINESS_POINT);
			transferJson = (JSONObject) hcpmsStorageService.getStorageDataMap(dto);
		}
		return transferJson;
	}

	@Override
	public boolean batchAddPointCutTemps(Map<String, Object> paramMap) throws BusinessException {
		JSONObject transferJson = this.getPointCutTempJson(paramMap);
		if (null != transferJson) {
			this.addIntegrityPointTemps(transferJson, (String) paramMap.get("userName"));// 临时扣分
			return true;
		}
		return false;
	}

	@Override
	public List<PointCutDTO> findPointCutStorageTemps(Map<String, Object> paramMap) throws BusinessException {
		List<PointCutDTO> pointCutTemps = new ArrayList<PointCutDTO>();
		return this.getPointCutTemps(paramMap, pointCutTemps);
	}

	@Override
	public List<PointCutDTO> findPointCutStorageCollections(Map<String, Object> paramMap) throws BusinessException {
		return getPointCutTempStorages(paramMap, integrityMedicalDao.findPointCutTemps(paramMap));
	}

	@Override
	public JSONObject getPointCutTempStorageJson(Map<String, Object> paramMap) {
		return this.getPointCutTempJson(paramMap);
	}

	@Override
	public List<IntegrityMedicalDTO> findIntegrityInsuranceUpdateDesc(Map<String, Object> paramMap)
			throws BusinessException {
		return integrityMedicalDao.findIntegrityInsuranceUpdateDesc(paramMap);
	}

	@Override
	public List<IntegrityMedicalDTO> findIntegrityInsuranceAuditApproveDesc(Map<String, Object> paramMap)
			throws BusinessException {
		return integrityMedicalDao.findIntegrityInsuranceAuditApproveDesc(paramMap);
	}

	@Override
	public List<IntegrityMedicalDTO> findIntegrityInsuranceDesc(Map<String, Object> paramMap) throws BusinessException {
		List<IntegrityMedicalDTO> integrityMedicals = null;
		String status = (String) paramMap.get("status");
		if (TrustConstants.STATUS_WAIT_MODIFIED.equals(status)) {// 待修改
			integrityMedicals = this.findIntegrityInsuranceUpdateDesc(paramMap);
		} else if (TrustConstants.STATUS_WAIT_AUDIT.equals(status) || TrustConstants.STATUS_WAIT_APPROVE.equals(status)) {// 待审核、待审批
			integrityMedicals = this.findIntegrityInsuranceAuditApproveDesc(paramMap);
		} else if (TrustConstants.STATUS_CLOSED.equals(status)) {// 已结案
			integrityMedicals = this.findIntegrityInsuranceClosedDesc(paramMap);
		}
		return integrityMedicals;
	}

	@Override
	public Map<String, Object> updateStorageSaveDatePoint(JSONObject transferJson, Map<String, Object> paramMap)
			throws BusinessException {
		Map<String, Object> result = new HashMap<String, Object>();
		String taskNo = transferJson.get("taskNo").toString();
		String userName = (String) paramMap.get("userName");
		if (StringUtils.isBlank(taskNo)) {
			taskNo = insuredMedicalDetailService.queryIntegrityNo();
			result.put("taskNo", taskNo);
		}
		if (StringUtils.isNotEmpty(taskNo) && StringUtils.isNotEmpty(userName)) {
			// 进行暂存操作
			JSONArray jsonArr = (JSONArray) transferJson.get("warnMarkTempDetails");
			if (jsonArr != null) {
				if (jsonArr.size() > 0) {
					JSONObject tempJson = jsonArr.getJSONObject(0);
					// 医务人员
					JSONArray deducitonStaffs = (JSONArray) tempJson.get("deducitonStaffs");// 医务人员录入记分有且只有一条记分值
					PointCutDTO dto = JSONObject.toJavaObject(deducitonStaffs.getJSONObject(0), PointCutDTO.class);
					if (dto != null) {
						dto.setCaseNo(taskNo);
						paramMap.put("taskNo", taskNo);
						transferJson.getJSONArray("warnMarkTempDetails").getJSONObject(0)
								.getJSONArray("deducitonStaffs").getJSONObject(0).put("caseNo", taskNo);
						result = this.saveIntegrityStorageData(transferJson, dto, paramMap, result);// 暂存临时信息
					}
				}
			}
		}
		insuredMedicalDetailService.updateCredentials(taskNo, transferJson);
		result.put(TrustConstants.STATE_KEY, TrustConstants.SUCCESS_STATE_VALUE);
		result.put(TrustConstants.MSG_KEY, TrustConstants.SAVE_SUCCESS);
		return result;
	}

	@Override
	public List<IntegrityMedicalDTO> findIntegrityInsuranceClosedDesc(Map<String, Object> paramMap)
			throws BusinessException {
		return integrityMedicalDao.findIntegrityInsuranceClosedDesc(paramMap);
	}

	/**
	 * 医务人员暂存临时信息
	 * 
	 * @param transferJson
	 * @param dto
	 * @param paramMap
	 * @param resultMap
	 * @return
	 * @throws BusinessException
	 */
	private Map<String, Object> saveIntegrityStorageData(JSONObject transferJson, PointCutDTO dto,
			Map<String, Object> paramMap, Map<String, Object> resultMap) throws BusinessException {
		integrityMedicalDao.deleteIntegrityStorageSaveData(paramMap);
		this.deletePointCutTemps(paramMap);// 医务人员记分信息清理
		paramMap.put("pointObj", TrustConstants.INTEGRITY_BUSINESS_POINT);
		int resultNum = this.countStorageSaveDatePoint(paramMap);
		if (resultNum > 0) {
			paramMap.put("point", dto.getPoint());
			integrityMedicalDao.updateStorageSaveDatePoint(paramMap);
		} else {
			HcpmsStorageDTO storageDto = new HcpmsStorageDTO();
			storageDto.setBusinessId((String) paramMap.get("taskNo"));
			storageDto.setModuleType(TrustConstants.CASE_TYPE);
			storageDto.setUserId((String) paramMap.get("userName"));
			storageDto.setBusinessType(TrustConstants.INTEGRITY_BUSINESS_POINT);
			storageDto.setDataMap(transferJson);
			storageDto.setAttribute1(dto.getPoint());// 医务人员手工扣分扣分值
			storageDto.setAttribute2(dto.getHappenDate());// 医务人员手工扣分案发日期
			storageDto.setAttribute3(dto.getTargetNo());// 医务人员手工扣分扣分对象编号
			storageDto.setAttribute4(dto.getOrgCode());// 医务人员手工扣分机构编号
			hcpmsStorageService.saveStorageData(storageDto, true);
		}
		return resultMap;
	}

	/**
	 * 医务人员记分信息清理
	 * 
	 * @param paramMap
	 * @throws BusinessException
	 */
	private void deletePointCutTemps(Map<String, Object> paramMap) throws BusinessException {
		String taskNo = (String) paramMap.get("taskNo");
		if (StringUtils.isNotEmpty(taskNo)) {
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("caseType", TrustConstants.CASE_TYPE);
			param.put("caseNo", taskNo);
			List<PointCutDTO> ids = this.findPointCutTemps(param);
			if (ids.size() > 0) {
				integrityMedicalDao.deletePointCut(ids);
			}
		}
	}

	/**
	 * 模块参数信息
	 * 
	 * @param transferJson
	 * @param pointCut
	 * @param caseNo
	 * @param caseType
	 * @return
	 */
	private PointCutDTO getPointCutDTOTemp(JSONObject transferJson, PointCutDTO pointCut, String caseNo, String caseType) {
		if (WarnConstants.TASK_TYPE.equals(caseType)) {// 预警
			HcpmsWarnsDTO warns = JSONObject.toJavaObject(transferJson.getJSONObject("warnInfo"), HcpmsWarnsDTO.class);
			pointCut.setHappenDate(warns.getEndDate());
			pointCut.setPunishDateStart(null);
			pointCut.setPunishDateEnd(null);
			pointCut.setCaseNo(caseNo);

		} else if (AuditConstants.MODULE_TYPE_02.equals(caseType)) {// 稽核
			pointCut.setHappenDate(transferJson.get("punishDateEnd").toString());// 案发时间录入预警或稽核截止时间
			pointCut.setPunishDateStart(null);// 惩罚起始
			pointCut.setPunishDateEnd(null);// 惩罚结束
			pointCut.setCaseNo(caseNo);
		} else if (TrustConstants.CASE_TYPE.equals(caseType)) {// 医务人员记分录入
			pointCut.setHappenDate(transferJson.get("happenDate").toString());// 案发时间录入预警或稽核截止时间
			pointCut.setPunishDateStart(transferJson.get("punishDateStart").toString());// 惩罚起始
			pointCut.setPunishDateEnd(transferJson.get("punishDateEnd").toString());// 惩罚结束
			pointCut.setCaseNo(caseNo);
		}
		return pointCut;
	}

	public void saveAuditOpinion(Map param) {
		integrityMedicalDao.saveAuditOpinion(param);

	}

	public void saveAuditBatchOpinion(List paramList) {
		integrityMedicalDao.saveAuditBatchOpinion(paramList);
	}

	/**
	 * 保存
	 * 
	 * @param transferJson
	 * @param userName
	 * @param caseNo
	 * @param caseType
	 * @return
	 */
	@Override
	@Transactional
	public Map<String, Object> saveIntegrityDoctor(JSONObject transferJson, String userName) throws BusinessException {
		Map<String, Object> result = new HashMap<String, Object>();
		if (transferJson == null || transferJson.equals(new JSONObject())) {
			return result;
		}
		String taskNo = transferJson.getString("taskNo");// 任务编号
		if (StringUtils.isBlank(taskNo)) {
			taskNo = insuredMedicalDetailService.queryIntegrityNo();
			result.put("taskNo", taskNo);
			// 扣分
			this.addIntegrityPoints(transferJson, userName, taskNo, TrustConstants.CASE_TYPE);
		} else {
			this.addIntegrityPointTemps(transferJson, userName);
		}
		// InsuranceTaskDTO taskDTO = new InsuranceTaskDTO();// 医务人员任务
		insuredMedicalDetailService.updateCredentials(taskNo, transferJson);
		result.put(TrustConstants.STATE_KEY, TrustConstants.SUCCESS_STATE_VALUE);
		result.put(TrustConstants.MSG_KEY, TrustConstants.SAVE_SUCCESS);
		return result;
	}

	/**
	 * 批量操作
	 * 
	 * @param transferJson
	 * @param userName
	 * @param caseNo
	 * @param caseType
	 * @return
	 */
	@Override
	@Transactional
	public Map<String, Object> saveIntegrityDoctorBatch(JSONObject transferJson, String userName, String taskNo)
			throws BusinessException {
		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		// 以下两个参数查询是否暂存用
		paramMap.put("warnNo", taskNo);// 任务编号
		paramMap.put("pointObj", TrustConstants.INTEGRITY_BUSINESS_POINT);
		// 以下三个参数查询暂存记分数据
		paramMap.put("caseType", TrustConstants.CASE_TYPE);// 任务编号
		paramMap.put("caseNo", taskNo);// 任务编号
		paramMap.put("userName", userName);// 任务编号
		// 判断是否是暂存
		try {
			int resultNum = this.countStorageSaveDatePoint(paramMap);
			if (resultNum > 0) {
				paramMap.put("userName", userName);
				transferJson = this.getPointCutTempStorageJson(paramMap);
				this.addIntegrityPointTemps(transferJson, userName);
			}
		} catch (BusinessException e) {
			result.put(TrustConstants.STATE_KEY, TrustConstants.FAILURE_STATE_VALUE);
			return result;
		}
		deleteStorageDateAll(taskNo, userName);
		result.put(TrustConstants.STATE_KEY, TrustConstants.SUCCESS_STATE_VALUE);
		result.put(TrustConstants.MSG_KEY, TrustConstants.SAVE_SUCCESS);
		return result;
	}

	@Override
	public int countStorageSaveDatePoint(Map<String, Object> paramMap) throws BusinessException {
		return integrityMedicalDao.countStorageSaveDatePoint(paramMap);
	}

	/**
	 * 删除暂存信息
	 * 
	 * @param warnNo
	 * @param userId
	 */
	public void deleteStorageDateAll(String warnNo, String userId) {
		HcpmsStorageDTO vo = new HcpmsStorageDTO();
		vo.setBusinessId(warnNo);
		vo.setModuleType(TrustConstants.CASE_TYPE);
		if (StringUtils.isNotBlank(userId)) {
			vo.setUserId(userId);
		}
		hcpmsStorageService.deleteStorageData(vo);
	}

}
